<template>
  <div class="container">
    <el-dialog
      title="盘  点"
      :visible.sync="dialog.centerDialogVisible"
      width="1400px"
      center
      @open="getData"
      :lock-scroll="true"
    >
      <el-table
        :data="formData"
        border
        style="width: 100%"
        empty-text="暂无数据"
      >
        <el-table-column
          prop="goods_name"
          label="名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="unit"
          label="单位"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="stock"
          label="库存"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="warehouse_name"
          label="仓库名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="warehouse_code"
          label="仓库编码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="qty"
          label="实际箱数"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="spec"
          label="规格"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="freeze_num"
          label="冻结数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="freeze_qty"
          label="冻结箱数"
          align="center"
        ></el-table-column>
        <el-table-column label="供应商" align="center" width="180">
          <template slot-scope="scope">
            <el-select
              size="small"
              v-model="scope.row.supplier_id"
              :multiple="false"
              filterable
              remote
              center
              reserve-keyword
              placeholder="请选择供应商"
              :remote-method="insideRemoteMethod"
              :loading="inside.loading"
              :disabled="!scope.row.status"
            >
              <el-option
                v-for="item in inside.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" width="180">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :precision="4"
              :controls="false"
              :disabled="scope.row.sumStatus"
              v-model="scope.row.inp_goods_num"
              @change="inpVal(scope)"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="箱数" align="center" width="180">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :precision="4"
              :controls="false"
              v-model="scope.row.total_qty"
              @change="inpQtyVal(scope)"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="价格" align="center" width="180">
          <template slot-scope="scope">
            <el-input-number
              v-if="scope.row.status"
              size="small"
              :precision="4"
              :controls="false"
              v-model="scope.row.inp_goods_price"
            ></el-input-number>
            <span v-else>按成本价出库</span>
          </template>
        </el-table-column>
        <el-table-column label="单皮" align="center" width="180">
          <template slot-scope="scope">
            <el-input-number
              size="small"
              :precision="4"
              :controls="false"
              v-model="scope.row.tare"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column
          label="非正常结算出库是否结算"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            <el-switch
              style="display: block"
              v-model="scope.row.is_statement"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="是"
              inactive-text="否"
            ></el-switch>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { supplierList } from "@/api/supplier/supplierList";

import { checkList, makeStorageBill, pickedList } from "@/api/check/check";
export default {
  props: {
    dialog: Object,
    formId: Array,
  },
  data() {
    return {
      formData: [],
      inside: {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false,
        id: "",
        buy_order_id: "",
      },
    };
  },
  methods: {
    getData() {
      this.formData = [];
      let data = {
        param: JSON.stringify(this.formId),
      };
      pickedList(data).then((res) => {
        if (res.code == 0) {
          res.data.forEach((arr) => {
            arr.inp_goods_num = "";
            arr.inp_goods_price = "";
            arr.total_qty = "";
            arr.status = false;
            arr.supplier_id = "";
            arr.tare = "";
            arr.is_statement = true;
          });
          this.formData = res.data;
        }
      });
      let datas = {
        page: 1,
        limit: 50,
      };
      //获取供应商列表
      supplierList(datas).then((res) => {
        this.inside.options = res.data.data.map((item) => {
          return { value: `${item.id}`, label: `${item.name}` };
        });
      });
    },
    // 嵌套搜索供应商数据
    insideRemoteMethod(query) {
      if (query !== "") {
        this.getSupplierList(query);
      } else {
        this.inside.options = [];
      }
    },
    //获取供应商数据
    getSupplierList(query) {
      let data = {
        name: query,
        page: 1,
        limit: 100,
      };
      this.inside.loading = true;
      supplierList(data).then((res) => {
        this.inside.list = res.data.data.map((item) => {
          return { value: `${item.id}`, label: `${item.name}` };
        });
        this.inside.loading = false;
        this.inside.options = this.inside.list.filter((item) => {
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      });
    },
    hideDialog() {
      this.dialog.centerDialogVisible = false;
    },
    //修改数量
    inpVal(scope) {
      if (
        scope.row.inp_goods_num == "" ||
        scope.row.inp_goods_num == 0 ||
        scope.row.inp_goods_num == undefined
      ) {
        scope.row.qtyStatus = false;
        scope.row.total_qty = 0.0;
      } else {
        if (scope.row.spec == 0) {
          scope.row.qtyStatus = true;
          scope.row.total_qty = 0;
        } else {
          scope.row.qtyStatus = true;
          scope.row.total_qty = (
            scope.row.inp_goods_num / scope.row.spec
          ).toFixed(4);
        }
      }
      if (scope.row.inp_goods_num > scope.row.stock) {
        scope.row.status = true;
      } else {
        scope.row.status = false;
        scope.row.inp_goods_price = "";
        scope.row.supplier_id = "";
      }
    },
    //修改箱数
    inpQtyVal(scope) {
      if (
        scope.row.total_qty == "" ||
        scope.row.total_qty == 0 ||
        scope.row.total_qty == undefined
      ) {
        scope.row.sumStatus = false;
      } else {
        // scope.row.sumStatus = true;
        // scope.row.inp_goods_num = ( scope.row.total_qty*scope.row.spec).toFixed(4)
      }
      if (scope.row.total_qty > scope.row.qty) {
        scope.row.status = true;
      } else {
        scope.row.status = false;
        scope.row.inp_goods_price = "";
      }
    },
    //提交
    confirm() {
      let bool = false;
      // this.formData.find(res => {
      //   if(res.supplier_id == ""){
      //     bool = true;
      //   }
      // });
      // if (bool) {
      //   this.$message({
      //     message: "请选择供应商",
      //     type: "info"
      //   });
      // } else {
      let center = [];
      this.formData.forEach((res) => {
        center.push({
          type:
            res.total_qty && res.inp_goods_num == "" ? "xiangshu" : "jingzhong",
          goods_id: res.goods_id,
          goods_erp_id: res.erp_goods_id,
          goods_erp_code: res.erp_goods_code,
          goods_name: res.goods_name,
          unit: res.unit,
          stock: res.stock,
          inp_goods_num: res.inp_goods_num,
          inp_goods_price: res.inp_goods_price,
          supplier_id: res.supplier_id,
          spec: res.spec,
          qty: res.qty,
          total_qty: res.total_qty,
          tare: res.tare,
          is_statement: res.is_statement ? 1 : 0,
          warehouse_code: res.warehouse_code,
          warehouse_name: res.warehouse_name,
        });
      });
      // console.log(center);
      // return;
      let data = {
        goods: center,
      };
      makeStorageBill(data)
        .then((res) => {
          if (res.code == 0) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.hideDialog();
            this.$emit("upData");
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
      // }
    },
    //取消
    cancel() {
      this.hideDialog();
    },
  },
};
</script>